﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC16BO
    /// </summary>
    public class VYC16BO:DBConnectionBO
    {
        public VYC16BO():base()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public List<ModelVYC16> GetAllList()
        {
            var list = new List<ModelVYC16>();
            const string query = "SELECT [Id],[IdNhomLienLac],[Ten],[GiaTri],[TraLoi],[TacGia],[NgayCapNhat],[TrangThai] FROM [VYC-16]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC16();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.IdNhomLienLac = (dataTable.Rows[i][1] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][1]);
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.GiaTri = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.TraLoi = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.TacGia = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][6] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][6];
                model.TrangThai = (dataTable.Rows[i][7] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][7]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC16 model)
        {
            var query = "INSERT INTO [VYC-16]([IdNhomLienLac],[Ten],[GiaTri],[TraLoi],[TacGia],[NgayCapNhat],[TrangThai]) VALUES (@idnhomlienlac, @ten, @giatri, @traloi, @tacgia, GETDATE(), @trangthai)";
            var sqlCommand = new SqlCommand(query, connection);

            sqlCommand.Parameters.Add("@idnhomlienlac", SqlDbType.Int).Value = model.IdNhomLienLac;
            sqlCommand.Parameters.Add("@ten", SqlDbType.NVarChar).Value = model.Ten;
            sqlCommand.Parameters.Add("@giatri", SqlDbType.NVarChar).Value = model.GiaTri;
            sqlCommand.Parameters.Add("@traloi", SqlDbType.NVarChar).Value = model.TraLoi;
            sqlCommand.Parameters.Add("@tacgia", SqlDbType.NVarChar).Value = model.TacGia;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.IdNhomLienLac;

            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC16 model)
        {
            const string query = "UPDATE [VYC-16] SET " +
                        "[IdNhomLienLac] = @idnhomlienlac" +
                        ",[Ten]= @ten" +
                        ",[GiaTri] = @giatri" +
                        ",[TraLoi]= @traloi" +
                        ",[TacGia]= @tacgia" +
                        ",NgayCapNhat = GETDATE(), TrangThai = @trangthai" +
                        " WHERE Id = @id";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@idnhomlienlac", SqlDbType.Int).Value = model.IdNhomLienLac;
            sqlCommand.Parameters.Add("@ten", SqlDbType.NVarChar).Value = model.Ten;
            sqlCommand.Parameters.Add("@giatri", SqlDbType.NVarChar).Value = model.GiaTri;
            sqlCommand.Parameters.Add("@traloi", SqlDbType.NVarChar).Value = model.TraLoi;
            sqlCommand.Parameters.Add("@tacgia", SqlDbType.NVarChar).Value = model.TacGia;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.TrangThai;
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = model.Id;

            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC16 model)
        {
            var query = "DELETE FROM [VYC-16] WHERE Id = @id" ;
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = model.Id;
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC16> GetById(int id)
        {
            var list = new List<ModelVYC16>();
            const string query = "SELECT [Id],[IdNhomLienLac],[Ten],[GiaTri],[TraLoi],[TacGia],[NgayCapNhat],[TrangThai] FROM [VYC-16] WHERE Id = @id";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
            var reader = sqlCommand.ExecuteReader();
            while (reader.Read())
            {
                var model = new ModelVYC16();

                model.Id = Convert.ToInt32(reader["Id"]);
                model.IdNhomLienLac = Convert.ToInt32(reader["IdNhomLienLac"]);
                model.Ten = reader["Ten"].ToString();
                model.GiaTri = reader["GiaTri"].ToString();
                model.TraLoi = reader["TraLoi"].ToString();
                model.TacGia = reader["TacGia"].ToString();
                model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Get by Id nhom lien lac
        /// </summary>
        /// <param name="idnhomlienlac"></param>
        /// <returns></returns>
        public List<ModelVYC16> GetByIdNhomlienlac(int idnhomlienlac)
        {
            var list = new List<ModelVYC16>();
            const string query = "SELECT [Id],[IdNhomLienLac],[Ten],[GiaTri],[TraLoi],[TacGia],[NgayCapNhat],[TrangThai] FROM [VYC-16] WHERE IdNhomLienLac = @idnhomlienlac";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@idnhomlienlac", SqlDbType.Int).Value = idnhomlienlac;
            var reader = sqlCommand.ExecuteReader();
            while (reader.Read())
            {
                var model = new ModelVYC16();

                model.Id = Convert.ToInt32(reader["Id"]);
                model.IdNhomLienLac = Convert.ToInt32(reader["IdNhomLienLac"]);
                model.Ten = reader["Ten"].ToString();
                model.GiaTri = reader["GiaTri"].ToString();
                model.TraLoi = reader["TraLoi"].ToString();
                model.TacGia = reader["TacGia"].ToString();
                model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];

                list.Add(model);
            }
            return list;
        }
    }
}